package com.cuz.bt;

import java.util.Stack;

public class T1 {


    static class TreeNode {
        TreeNode left;
        TreeNode right;
        int val;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        t1.left = t2;
        t1.right = t3;
        TreeNode t4 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        t2.left = t4;
        t2.right = t5;

        process3(t1);
    }

    public static void process3(TreeNode r) {
        if (r == null) {
            return;
        }

        Stack<TreeNode> help = new Stack<>();
        Stack<TreeNode> collect = new Stack<>();
        help.push(r);
        while (!help.isEmpty()) {
            TreeNode temp = help.pop();
            collect.push(temp);
            if (temp.left != null) {
                help.push(temp.left);
            }
            if (temp.right != null) {
                help.push(temp.right);
            }
        }

        StringBuilder sb = new StringBuilder();
        while (!collect.isEmpty()) {
            sb.append(collect.pop().val).append(",");
        }
        System.out.println(sb);
    }
}
